<?php
namespace app\admin\controller;

use think\Controller;
use think\Session;
use think\Request;
use app\admin\model\GoodsModel;

class Goods extends Common
{
	/**
	*	商品列表
	*/
	public function index()
	{
		$data = $this->request->param();
		$GoodsModel = new GoodsModel();
		// 查询商品分类
		$category = $GoodsModel->goodsCategory();
		$this->assign('category',$category);
		// 商品列表
		$list = $GoodsModel->getGoods($data);
		$this->assign('list',$list);
		$page = $list->render();
		$this->assign('page',$page);

		$this->assign('cid',isset($data['cid']) ? $data['cid'] : '');
		$this->assign('g_name',isset($data['g_name']) ? $data['g_name'] : '');
		$this->assign('g_show',isset($data['g_show']) ? $data['g_show'] : '');
		$this->assign('g_status',isset($data['g_status']) ? $data['g_status'] : '');
		return $this->fetch();
	}

	/**
	*	商品规格列表
	*/	
	public function info()
	{
		$data = $this->request->param();
		$GoodsModel = new GoodsModel();
		$list = $GoodsModel->getInfo($data['id']);
		$this->assign('list',$list);
		$page = $list->render();
		$this->assign('page',$page);
		return $this->fetch();
	}

	/**
	*	每个商品的规格
	*/	
	public function getinfo()
	{
		$data = $this->request->param();
		$GoodsModel = new GoodsModel();
		$list = $GoodsModel->getOneInfos($data['id']);
		if($list){
			return returnJson($code=0,$msg='获取成功',$list);
		}else{
			return returnJson($code=1,$msg='获取失败',$list);
		}
	}

	/**
	*	添加商品
	*/
	public function add()
	{
		$GoodsModel = new GoodsModel();

		// 查询商品分类
		$category = $GoodsModel->goodsCategory();
		$this->assign('category',$category);

		return $this->fetch();
	}

	/**
	*	添加商品提交
	*/	
	public function addPost()
	{
		if($this->request->isPost()){
			$data = $this->request->param();
			$GoodsModel = new GoodsModel();
			$result = $this->validate($data, 'GoodsValidate');

	        if ($result !== true) {

	            $this->error($result);
	        }
			$list = $GoodsModel->addGoods($data);
			if($list){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}
	}

	/**
	*	修改商品信息
	*/
	public function edit()
	{
		$data = $this->request->param();
		if($data){
			$GoodsModel = new GoodsModel();
			$list = $GoodsModel->getOne($data['id']);
			// 查询商品分类
			$category = $GoodsModel->goodsCategory();
			$this->assign('category',$category);
			$post = [];
			//把pc中的图片json转为数组
			$post = json_decode($list['g_pic'],true);
			$this->assign('post',$post);
			$this->assign('list',$list);
		}
		return $this->fetch();
	}

	/**
	*	删除商品的规格
	*/
	public function deleteInfo()
	{
		$data = $this->request->param();
		if($data){
			$GoodsModel = new GoodsModel();
			$list = $GoodsModel->deleteInfo($data['id']);
			if($list){
				return returnJson($code=0,$msg='获取成功',$list);
			}else{
				return returnJson($code=1,$msg='获取失败',$list);
			}
		}
	}

	/**
	*	修改商品信息提交
	*/	
	public function editPost()
	{
		if($this->request->isPost()){
			$data = $this->request->param();
			$GoodsModel = new GoodsModel();
			$result = $this->validate($data, 'GoodsValidate');

	        if ($result !== true) {

	            $this->error($result);
	        }

			$list = $GoodsModel->editGoods($data['id'],$data);

			$this->success('修改成功');
		}
	}

	/*
	*	删除商品
	*/
	public function delete()
	{
		$data = $this->request->param();
		if($data){
			$GoodsModel = new GoodsModel();
			$list = $GoodsModel->deleteGoods($data['id']);
			if($list){
				$this->success('删除成功','/admin/Goods/index');
			}else{
				$this->error('删除失败');
			}
		}
	}


/*****************************************************   团购商品区域   *********************************************************************/


	/**
	*	团购商品列表
	*/
	public function indexGroup()
	{
		$data = $this->request->param();
		$GoodsModel = new GoodsModel();
		// 查询商品分类
		$category = $GoodsModel->goodsCategory();
		$this->assign('category',$category);
		// 团购商品列表
		$list = $GoodsModel->getGroupGoods($data);
		$this->assign('list',$list);
		$page = $list->render();
		$this->assign('page',$page);

		$this->assign('cid',isset($data['cid']) ? $data['cid'] : '');
		$this->assign('g_name',isset($data['g_name']) ? $data['g_name'] : '');
		$this->assign('g_show',isset($data['g_show']) ? $data['g_show'] : '');
		$this->assign('g_status',isset($data['g_status']) ? $data['g_status'] : '');
		return $this->fetch();
	}

	/**
	*	添加团购商品
	*/
	public function addGroup()
	{
		$GoodsModel = new GoodsModel();

		// 查询商品分类
		$category = $GoodsModel->goodsCategory();
		$this->assign('category',$category);

		return $this->fetch();
	}

	/**
	*	添加团购商品提交
	*/
	public function addGroupPost()
	{
		if($this->request->isPost()){
			$data = $this->request->param();
			$GoodsModel = new GoodsModel();
			$result = $this->validate($data, 'GoodsGroupValidate');

	        if ($result !== true) {

	            $this->error($result);
	        }
			$list = $GoodsModel->addGroupGoods($data);
			if($list){
				$this->success('添加成功');
			}else{
				$this->error('添加失败');
			}
		}
	}

	/**
	*	修改团购商品
	*/
	public function editGroup()
	{
		$data = $this->request->param();
		if($data){
			$GoodsModel = new GoodsModel();
			$list = $GoodsModel->getGroupOne($data['id']);
			// 查询商品分类
			$category = $GoodsModel->goodsCategory();
			$this->assign('category',$category);
			$post = [];
			//把pc中的图片json转为数组
			$post = json_decode($list['g_pic'],true);
			$this->assign('post',$post);
			$this->assign('list',$list);
		}
		return $this->fetch();
	}

	/**
	*	修改团购商品提交
	*/
	public function editGroupPost()
	{
		if($this->request->isPost()){
			$data = $this->request->param();
			$GoodsModel = new GoodsModel();
			$result = $this->validate($data, 'GoodsGroupValidate');

	        if ($result !== true) {

	            $this->error($result);
	        }
			$list = $GoodsModel->editGroupGoods($data['id'],$data);

			$this->success('修改成功');
		}
	}

	/**
	*	删除团购商品
	*/
	public function deleteGroup()
	{
		$data = $this->request->param();
		if($data){
			$GoodsModel = new GoodsModel();
			$list = $GoodsModel->deleteGroupGoods($data['id']);
			if($list){
				$this->success('删除成功','/admin/Goods/indexGroup');
			}else{
				$this->error('删除失败');
			}
		}
	}


	/**
	*	批量发布
	*/
	public function racking()
	{
		$data = $this->request->param();
		$GoodsModel = new GoodsModel();
		$list = $GoodsModel->racking($data);
		if($list){
			return returnJson($code=1,$msg='修改成功',$list);
		}else{
			return returnJson($code=1,$msg='修改成功',$list);
		}
	}

	/**
	*	批量发布
	*/
	public function rack()
	{
		if($this->request->isPost()){
			$data = $this->request->param();
			$GoodsModel = new GoodsModel();
			$list = $GoodsModel->rack($data);
			if($list){
				return returnJson($code=1,$msg='修改成功',$list);
			}else{
				return returnJson($code=1,$msg='修改成功',$list);
			}
		}
	}
}
